package com.zsrt.java.day20161227.zp.demo05;

import java.util.Arrays;

/**
 * //charAt(2)
 * @author zsrt
 *
 */
public class DouNiu {

	public static void main(String[] args) {
		
		String[] poker = new String[52];
		
		poker[0] = "方块3";
		poker[1] = "梅花3";
		poker[2] = "红桃3";
		poker[3] = "黑桃3";
		
		poker[4] = "方块4";
		poker[5] = "梅花4";
		poker[6] = "红桃4";
		poker[7] = "黑桃4";
		
		poker[8] = "方块5";
		poker[9] = "梅花5";
		poker[10] = "红桃5";
		poker[11] = "黑桃5";
		
		poker[12] = "方块6";
		poker[13] = "梅花6";
		poker[14] = "红桃6";
		poker[15] = "黑桃6";
		
		poker[16] = "方块7";
		poker[17] = "梅花7";
		poker[18] = "红桃7";
		poker[19] = "黑桃7";
		
		poker[20] = "方块8";
		poker[21] = "梅花8";
		poker[22] = "红桃8";
		poker[23] = "黑桃8";
		
		poker[24] = "方块9";
		poker[25] = "梅花9";
		poker[26] = "红桃9";
		poker[27] = "黑桃9";
		
		poker[28] = "方块10";
		poker[29] = "梅花10";
		poker[30] = "红桃10";
		poker[31] = "黑桃10";
		
		poker[32] = "方块J";
		poker[33] = "梅花J";
		poker[34] = "红桃J";
		poker[35] = "黑桃J";
		
		poker[36] = "方块Q";
		poker[37] = "梅花Q";
		poker[38] = "红桃Q";
		poker[39] = "黑桃Q";
		
		poker[40] = "方块K";
		poker[41] = "梅花K";
		poker[42] = "红桃K";
		poker[43] = "黑桃K";
		
		poker[44] = "方块A";
		poker[45] = "梅花A";
		poker[46] = "红桃A";
		poker[47] = "黑桃A";
		
		poker[48] = "方块2";
		poker[49] = "梅花2";
		poker[50] = "红桃2";
		poker[51] = "黑桃2";
		
		String[] player01 = new String[5];
		String[] player01V =new String[5];
		int[] player01I =new int[5];
		player01 = deal(player01,poker);
		player01V = forValue(player01);
		player01I=forValueAgain(player01V);
		
		String[] player02 = new String[5];
		String[] player02V =new String[5];
		int[] player02I =new int[5];
		player02 = deal(player02,poker);
		player02V = forValue(player02);
		player02I=forValueAgain(player02V);
		
		String[] player03 = new String[5];
		String[] player03V =new String[5];
		int[] player03I =new int[5];
		player03 = deal(player03,poker);
		player03V = forValue(player03);
		player03I=forValueAgain(player03V);
		
		String[] player04 = new String[5];
		String[] player04V =new String[5];
		int[] player04I =new int[5];
		player04 = deal(player04,poker);
		player04V = forValue(player04);
		player04I=forValueAgain(player04V);
		
		System.out.print("玩家1的牌为："+Arrays.toString(player01));
		System.out.println();
		System.out.print("转化后为："+Arrays.toString(player01I));
		judge(player01I);
		System.out.println();
		System.out.println("----------------------------------------------------");
		
		System.out.print("玩家2的牌为："+Arrays.toString(player02));
		System.out.println();
		System.out.print("转化后为："+Arrays.toString(player02I));
		judge(player02I);
		System.out.println();
		System.out.println("----------------------------------------------------");
		
		System.out.print("玩家3的牌为："+Arrays.toString(player03));
		System.out.println();
		System.out.print("转化后为："+Arrays.toString(player03I));
		judge(player03I);
		System.out.println();
		System.out.println("----------------------------------------------------");
		
		System.out.print("玩家4的牌为："+Arrays.toString(player04));
		System.out.println();
		System.out.print("转化后为："+Arrays.toString(player04I));
		judge(player04I);
		System.out.println();
		System.out.println("----------------------------------------------------");
		
	}
	
	/**
	 * 发牌 每位玩家5张牌
	 * @param player 玩家手牌数组
	 * @param poker  牌堆数组
	 * @return
	 */
	public static String[] deal(String[] player,String[] poker){
		
		//发5张牌
		for(int i=0;i<player.length;i++){
			
			int ran = (int) (Math.random()*poker.length);
			//得到一张牌
			player[i] = poker[ran];
			
			String[] newArray = new String[poker.length-1];
			
			//1：获取 被删除的数 的下标 index ==ran
			//2:以下标为基准 把 原来的数组oldArray 分为2部分  循环原来的数组
			//2.1 如果在index之前   原样复制到  新的数组中
			for(int j=0;j<ran;j++){
				String v = poker[j];
				newArray[j]=v;
			}
			
			//2.1 如果在index之后   把index-1 再复制
			for(int k=ran+1;k<poker.length;k++){
				String v = poker[k];
				newArray[k-1]=v;
			}
			
			 poker = newArray;
		
	    }
		return player;
	}
	
	/**
	 * 将牌中的A J Q K 分别转化为 1 10 10 10
	 * @param array  目标数组 
	 * @return
	 */
	public static String[] forValue(String[] array){
		
		String[] value = new String[array.length];
		
		//charAt(2)
		for(int i=0;i<array.length;i++){
			char temp=array[i].charAt(2);
			String str = "";
			str = String.valueOf(temp);
			if(str.equals("1")){
				str = "10";
			}
			value[i] = str;
		}
		return value;
	}
	
	/**
	 * 将String类型的牌转化为   int类型
	 * @param array   目标数组
	 * @return
	 */
	public static int[] forValueAgain(String[] array){
		
		String[] value = new String[array.length];
		
		int[] intValue = new int[array.length];
		
		for(int i =0;i<array.length;i++){
			if(array[i].equals("A")){
				value[i] = "1";
			}else if(array[i].equals("J")){
				value[i] = "10";
			}else if(array[i].equals("Q")){
				value[i] = "10";
			}else if(array[i].equals("K")){
				value[i] = "10";
			}else{
				value[i]=array[i];
			}
		}
	
		for(int j =0;j<value.length;j++){
			int tempValue = Integer.parseInt(value[j]);
			intValue[j] = tempValue;
		}
		
		//System.out.println(Arrays.toString(intValue));
		return intValue;
	}
	
	/**
	 * 判断5张牌中有没有牛，如果有，最大是牛几？
	 * @param intArray
	 */
	public static void judge(int[] intArray){
		
		int[] sum = new int[9];
		int[] left = new int[9];
		
		sum[0] = intArray[0]+intArray[1]+intArray[2];
		sum[1] = intArray[0]+intArray[1]+intArray[3];
		sum[2] = intArray[0]+intArray[1]+intArray[4];
		sum[3] = intArray[0]+intArray[2]+intArray[3];
		sum[4] = intArray[0]+intArray[2]+intArray[4];
		sum[5] = intArray[0]+intArray[3]+intArray[4];
		sum[6] = intArray[1]+intArray[2]+intArray[3];
		sum[7] = intArray[1]+intArray[2]+intArray[4];
		sum[8] = intArray[2]+intArray[3]+intArray[4];
		
		left[0] =intArray[3]+intArray[4];
		left[1] =intArray[2]+intArray[4];
		left[2] =intArray[2]+intArray[3];
		left[3] =intArray[1]+intArray[4];
		left[4] =intArray[1]+intArray[3];
		left[5] =intArray[1]+intArray[2];
		left[6] =intArray[0]+intArray[4];
		left[7] =intArray[0]+intArray[3];
		left[8] =intArray[0]+intArray[1];
		
		int j = 0;
		int yn =0;
		int[] index = new int[9];
		
		//有牛的下标数放入index数组，从index[0]开始
		for(int i=0;i<sum.length;i++){
			if(sum[i]%10==0){
				index[j] = i;
				j++;
				yn++;
			}
		}
		
		if(yn==0){
			System.out.println("没有牛！");
		}else{
			int[] niu = new int[9];
			for(int begin=0;begin<index.length;begin++){
				if(index[begin]!= 0){
					niu[begin]=left[index[begin]]%10;
				}
			}
			int maxNiu = niu[0];
			for(int k=0;k<niu.length;k++){
				if(maxNiu<niu[k]){
					maxNiu = niu[k];
				}
			}
			System.out.println("有牛，最大的牛为：牛"+maxNiu);
		}
		
	}
	

}
